<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <%@ include file="../../include/head.jsp"%>
    <%@ include file="../../include/layui.jsp"%>
    <title>代码生成工具</title>
</head>
<body style="" layadmin-themealias="default">

<div class="layui-fluid">
    <form class="layui-form" >
        <div class="layui-card">
            <div class="layui-card-header">
                <div class="layui-form-item layui-inline">
                    <label>表名:</label>
                    <div class="layui-inline">
                        <input class="layui-input" name="tableName" id="tableNameInput" autocomplete="off">
                    </div>
                    <button type="button" class="layui-btn layui-btn-radius btn-min-width" id="search-btn">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
                <div class="layui-form-item"></div>
            </div>
            <div class="layui-card-body layui-form-pane" >
                <div class="layui-form-item ">
                    <div class="layui-row">
                        <div class="layui-col-md6">
                                <label class="layui-form-label">表名</label>
                                <div class="layui-input-block" >
                                    <input class="layui-input" name="tableName" id="buildCodeTableName" disabled >
                                </div>
                        </div>
                        <div class="layui-col-md6">
                            <label class="layui-form-label">对象名</label>
                            <div class="layui-input-block">
                                <input class="layui-input" name="javaName" id="buildCodeObjectName" lay-verify="required" placeholder="请输入对象名字">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item ">
                    <div class="layui-row">
                        <div class="layui-col-md6">
                            <label class="layui-form-label">主键</label>
                            <input type="checkbox" name="autoSeq" id="buildCodeAutoSeq" value="true" lay-skin="switch" lay-text="自增长|自定义" checked>
                            <div  class="layui-input-inline">
                                <input class="layui-input" id="buildCodeSequenceName" name="sequenceName" lay-verify="buildCodeSequenceName" placeholder="主键为序列时请输入序列名字">
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <label class="layui-form-label">描述</label>
                            <div class="layui-input-block">
                                <input class="layui-input" name="description" id="buildCodeDescription" lay-verify="buildCodeDescription" placeholder="请输入描述">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item ">
                    <div class="layui-row">
                        <div class="layui-col-md6">
                            <label class="layui-form-label">模块</label>
                            <div class="layui-input-block" >
                                <select name="module" id="module"  lay-filter="module">
                                    <option value="user" selected>user</option>
                                    <option value="oauth">oauth</option>
                                    <option value="sec">sec</option>
                                    <option value="pad">pad</option>
                                    <option value="order">order</option>
                                    <option value="pay">pay</option>
                                    <option value="config">config</option>
                                    <option value="resource">resource</option>
                                    <option value="market">market</option>
                                    <option value="desktop">desktop</option>
                                    <option value="log">log</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <label class="layui-form-label">包名</label>
                            <div class="layui-input-block">
                                <input class="layui-input" name="packageName" id="buildCodePackageName" >
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label  class="layui-form-label">生成类型</label>
                    <div class="layui-form-checkbox">
                        <input type="checkbox" name="createType" value="code" lay-skin="primary" title="代码" checked>
                        <input type="checkbox" name="createType" value="listView" lay-skin="primary" title="主界面" checked>
                        <input type="checkbox" name="createType" value="editView" lay-skin="primary" title="编辑界面" checked>
                        <input type="checkbox" name="createType" value="menuSql" lay-skin="primary" title="菜单脚本" checked>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-card" >
            <div class="layui-tab layui-tab-card">
                <ul class="layui-tab-title">
                    <li class="layui-this">Model属性</li>
                    <li>页面属性</li>
                    <li>表单属性</li>
                </ul>
                <div class="layui-tab-content" >
                    <div class="layui-tab-item layui-show" >
                        <div id="table-build-code">
                            <table class="layui-hide" id="table-code" lay-filter="view"></table>
                        </div>
                    </div>
                    <div class="layui-tab-item">
                        <div class="layui-form-item">
                            <label class="layui-form-label">展示方式</label>
                            <div class="layui-form-checkbox">
                                <input type="checkbox" name="showType" value="checkBox" lay-skin="primary" title="是否带复选框" checked>
                                <input type="checkbox" name="showType" value="page" lay-skin="primary" title="是否分页" checked>
                            </div>
                        </div>
                        <hr/>
                        <div id="table-build-view">
                            <table class="layui-hide" id="table-view" lay-filter="view"></table>
                        </div>
                    </div>
                    <div class="layui-tab-item">
                        <div id="table-build-edit">
                            <table class="layui-hide" id="table-edit" lay-filter="view"></table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-row">
                    <div class="layui-col-md5">&nbsp;</div>
                    <div class="layui-col-md2">
                        <div class="layui-input-block">
                            <button class="layui-btn" id="formSubmit" lay-submit lay-filter="*">立即提交</button>
                        </div>
                    </div>
                    <div class="layui-col-md5">&nbsp;</div>
                </div>
            </div>
        </div>
    </form>
</div>
<script type="text/html" id="notNullTpl">
    {{#  if(d.notNull){ }}
    <input type="checkbox" name="buildCodeTables[{{d.LAY_INDEX-1}}].notNull" value="true" lay-skin="switch" lay-text="是|否"  checked >
    {{#  } else { }}
    <input type="checkbox" name="buildCodeTables[{{d.LAY_INDEX-1}}].notNull" value="true"  lay-skin="switch" lay-text="是|否"  >
    {{#  } }}
</script>
<script type="text/html" id="pkTpl">
    {{#  if(d.pk){ }}
    <input type="checkbox" name="buildCodeTables[{{d.LAY_INDEX-1}}].pk" value="true"  lay-skin="switch" lay-text="是|否"  checked >
    {{#  } else { }}
    <input type="checkbox" name="buildCodeTables[{{d.LAY_INDEX-1}}].pk" value="true"  lay-skin="switch" lay-text="是|否"  >
    {{#  } }}
</script>
<script type="text/html" id="showListTpl">
    {{#  if(d.showList){ }}
    <input type="checkbox" name="buildCodeViews[{{d.LAY_INDEX-1}}].showList" value="true" lay-skin="switch" lay-text="是|否" checked >
    {{#  } else { }}
    <input type="checkbox" name="buildCodeViews[{{d.LAY_INDEX-1}}].showList" value="true"  lay-skin="switch" lay-text="是|否" >
    {{#  } }}
</script>
<script type="text/html" id="showQueryTpl">
    {{#  if(d.showQuery){ }}
    <input type="checkbox" name="buildCodeViews[{{d.LAY_INDEX-1}}].showQuery" value="true"  lay-skin="switch" lay-filter="switchTest" lay-text="是|否" checked >
    {{#  } else { }}
    <input type="checkbox" name="buildCodeViews[{{d.LAY_INDEX-1}}].showQuery"  value="true"  lay-skin="switch" lay-filter="switchTest" lay-text="是|否" >
    {{#  } }}
</script>
<script type="text/html" id="queryVerifyTpl">
    <select name="buildCodeViews[{{d.LAY_INDEX-1}}].verifyType" class="layui-input" lay-ignore >
        <option value="REGULAR" {{#  if(d.verifyType=="REGULAR"){ }} selected{{# }}} >正则</option>
        <option value="NOTNULL"  {{#  if(d.verifyType=="NOTNULL"){ }} selected{{# }}}>非空</option>
        <option value="MAX" {{#  if(d.verifyType=="MAX"){ }} selected{{# }}}>最大</option>
        <option value="MIN" {{#  if(d.verifyType=="MIN"){ }} selected{{# }}}>最小</option>
        <option value="SIZE" {{#  if(d.verifyType=="SIZE"){ }} selected{{# }}}>长度</option>
        <option value="DATE" {{#  if(d.verifyType=="DATE"){ }} selected{{# }}}>日期</option>
    </select>
</script>
<script type="text/html" id="showEditTpl">
    {{#  if(d.showEdit){ }}
    <input type="checkbox" name="buildCodeEdits[{{d.LAY_INDEX-1}}].showEdit" value="true" lay-skin="switch" lay-text="DISPLAY|HIDDEN" checked >
    {{#  } else { }}
    <input type="checkbox" name="buildCodeEdits[{{d.LAY_INDEX-1}}].showEdit" value="true" lay-skin="switch" lay-text="DISPLAY|HIDDEN" >
    {{#  } }}
</script>
<script type="text/html" id="editVerifyTpl">
    <select name="buildCodeEdits[{{d.LAY_INDEX-1}}].verifyType" class="layui-input" lay-ignore >
        <option value="REGULAR" {{#  if(d.verifyType=="REGULAR"){ }} selected{{# }}} >正则</option>
        <option value="NOTNULL"  {{#  if(d.verifyType=="NOTNULL"){ }} selected{{# }}}>非空</option>
        <option value="MAX" {{#  if(d.verifyType=="MAX"){ }} selected{{# }}}>最大</option>
        <option value="MIN" {{#  if(d.verifyType=="MIN"){ }} selected{{# }}}>最小</option>
        <option value="SIZE" {{#  if(d.verifyType=="SIZE"){ }} selected{{# }}}>长度</option>
        <option value="DATE" {{#  if(d.verifyType=="DATE"){ }} selected{{# }}}>日期</option>
    </select>
</script>
<script type="text/html" id="showFlagTpl">
    <select name="buildCodeViews[{{d.LAY_INDEX-1}}].showFlag" class="layui-input" lay-ignore >
        <option value="VALUE" {{#  if(d.showFlag=="VALUE"){ }} selected{{# }}}>原值展示</option>
        <option value="TRANS" {{#  if(d.showFlag=="TRANS"){ }} selected{{# }}}>转义</option>
    </select>
</script>
<script type="text/html" id="showFlagTransTypeTpl">
    <select name="buildCodeViews[{{d.LAY_INDEX-1}}].showFlagTransType" class="layui-input" lay-ignore >
        <option value="" {{#  if(d.showFlagTransType!="JS" && d.showFlagTransType=="JAVA" ){ }} selected{{# }}}>请选择</option>
        <option value="JS" {{#  if(d.showFlagTransType=="JS"){ }} selected{{# }}}>前端</option>
        <option value="JAVA" {{#  if(d.showFlagTransType=="JAVA"){ }} selected{{# }}}>后端</option>
    </select>
</script>
<script>
    layui.use(['index', 'table', 'form'], function(){
        var table = layui.table;
        var form = layui.form;

        table.render({
            elem: '#table-code'
            ,url:  '${ctx}/buildCode/getTableView'
            ,cols: [[
                {field:'columnName', fixed:'left', title: '字段名',templet:'<div>{{d.columnName}}</div>'}
                ,{field:'propertyName', title: '属性名',templet:'<div><input type="hidden" class="layui-input" name="buildCodeTables[{{d.LAY_INDEX-1}}].columnName" value="{{d.columnName}}"><input type="text" class="layui-input" name="buildCodeTables[{{d.LAY_INDEX-1}}].propertyName" value="{{d.propertyName}}"></div>'}
                ,{field:'columnComment', title: '描述',templet:'<div><input type="text" class="layui-input" name="buildCodeTables[{{d.LAY_INDEX-1}}].columnComment" value="{{d.columnComment}}"></div>'}
                ,{field:'length', title: '长度',templet:'<div><input type="text" class="layui-input" name="buildCodeTables[{{d.LAY_INDEX-1}}].propertyLength" value="{{d.propertyLength}}"></div>'}
                ,{field:'dbType', title: '数据库类型',templet:'<div><input type="text" class="layui-input" name="buildCodeTables[{{d.LAY_INDEX-1}}].dbType" value="{{d.dbType}}"></div>'}
                ,{field:'javaType', title: 'java类型',templet:'<div><input type="text" class="layui-input" name="buildCodeTables[{{d.LAY_INDEX-1}}].javaType" value="{{d.javaType}}"></div>'}
                ,{field:'isPK', title: '主键',templet:  pkTpl}
                ,{field:'isNotNull', title: '非空',templet:  pkTpl}
            ]]
        });
        table.render({
            elem: '#table-view'
            ,cols: [[
                {field:'columnName', title: '字段名', width: 120,fixed:'left',templet:'<div>{{d.columnName}}</div>'}
                ,{field:'isShowList', title: '列表', width: 120,templet:  showListTpl,}
                ,{field:'isShowQuery', title: '查询条件', width: 120,templet:  showQueryTpl}
                ,{field:'showFlag', title: '显示方式', width: 120,templet:  showFlagTpl}
                ,{field:'showFlagTransType', title: '转义类型', width: 120,templet:  showFlagTransTypeTpl}
                ,{field:'showFlagValue', title: '转义值', width: 120,templet:  '<div><input type="text" class="layui-input" name="buildCodeViews[{{d.LAY_INDEX-1}}].showFlagValue" value="{{d.showFlagValue}}"></div>'}
                ,{field:'queryFlag', title: '查询展示方式', width: 120,templet:  '<div><select name="buildCodeViews[{{d.LAY_INDEX-1}}].queryFlag" class="layui-input" lay-ignore >'+
                    '<option value="TEXT" selected>文本框</option>'+
                    '<option value="SELECT" >下拉框</option>'+
                    '<option value="CHECKBOX" >复选框</option>'+
                    '<option value="RODIO" >单选</option>'+
                    '</select><div>'
                }
                ,{field:'queryType', title: '查询方式', width: 120,templet:  '<div><select name="buildCodeViews[{{d.LAY_INDEX-1}}].queryType" class="layui-input" lay-ignore >'+
                    '<option value="EQUAL" selected>=</option>'+
                    '<option value="UNEQUAL" >&lt;</option>'+
                    '<option value="LESS" >&lt;</option>'+
                    '<option value="GREATER" >&gt;</option>'+
                    '<option value="BETWEEN" >between and</option>'+
                    '</select><div>'
                }
                ,{field:'queryFlagShowValue', title: '查询展示值', width: 120,templet:  '<div><input type="text" class="layui-input" name="buildCodeViews[{{d.LAY_INDEX-1}}].queryFlagValue" value="{{d.queryFlagValue}}"></div>'}
                ,{field: 'verifyType',title: '验证方式', width: 120,templet: queryVerifyTpl}
                ,{field:'jsFormRegular', title: '前端验证', width: 120,templet:  '<div><input type="text" class="layui-input" name="buildCodeViews[{{d.LAY_INDEX-1}}].jsFormRegular" value="{{d.jsFormRegular}}"></div>'}
                ,{field:'jsFormRegularMessage', title: '前端提示语句', width: 120,templet:  '<div><input type="text" class="layui-input" name="buildCodeViews[{{d.LAY_INDEX-1}}].jsFormRegularMessage" value="{{d.jsFormRegularMessage}}"></div>'}
                ,{field:'javaFormRegular', title: '后端验证', width: 120,templet:  '<div><input type="text" class="layui-input" name="buildCodeViews[{{d.LAY_INDEX-1}}].javaFormRegular" value="{{d.javaFormRegular}}"></div>'}
                ,{field:'javaFormRegularMessage', title: '后台提示语句',width: 120,templet:  '<div><input type="hidden" class="layui-input" name="buildCodeViews[{{d.LAY_INDEX-1}}].columnName" value="{{d.columnName}}"><input type="text" class="layui-input" name="buildCodeViews[{{d.LAY_INDEX-1}}].javaFormRegularMessage" value="{{d.javaFormRegularMessage}}"></div>'}
            ]]
        });


        table.render({
            elem: '#table-edit'
            ,cols: [[
                {field:'columnName', title: '字段名',fixed:'left',templet:'<div>{{d.columnName}}</div>'}
                ,{field:'showEdit',title:'展示方式',templet:showEditTpl}
                ,{field:'editFlag', title: '编辑方式',templet:  '<div><select name="buildCodeEdits[{{d.LAY_INDEX-1}}].editFlag" class="layui-input"  lay-ignore>'+
                    '<option value="TEXT" selected>文本框</option>'+
                    '<option value="SELECT" >下拉框</option>'+
                    '<option value="CHECKBOX" >复选框</option>'+
                    '<option value="RODIO" >单选</option>'+
                    '</select><div>'
                }
                ,{field:'editFlagShowValue', title: '编辑展示值',templet:  '<div><input type="text" class="layui-input" name="buildCodeEdits[{{d.LAY_INDEX-1}}].editFlagValue" value="{{d.editFlagValue}}"></div>'}
                ,{field: 'verifyType',title: '验证方式', width: 120,templet: editVerifyTpl}
                ,{field:'jsFormRegular', title: '前端验证', width: 120,templet:  '<div><input type="text" class="layui-input" name="buildCodeEdits[{{d.LAY_INDEX-1}}].jsFormRegular" value="{{d.jsFormRegular}}"></div>'}
                ,{field:'jsFormRegularMessage', title: '前端提示语句', width: 120,templet:  '<div><input type="text" class="layui-input" name="buildCodeEdits[{{d.LAY_INDEX-1}}].jsFormRegularMessage" value="{{d.jsFormRegularMessage}}"></div>'}
                ,{field:'javaFormRegular', title: '后端验证', width: 120,templet:  '<div><input type="text" class="layui-input" name="buildCodeEdits[{{d.LAY_INDEX-1}}].javaFormRegular" value="{{d.javaFormRegular}}"></div>'}
                ,{field:'javaFormRegularMessage', title: '后台提示语句',width: 120,templet:  '<div><input type="hidden" class="layui-input" name="buildCodeEdits[{{d.LAY_INDEX-1}}].columnName" value="{{d.columnName}}"><input type="text" class="layui-input" name="buildCodeEdits[{{d.LAY_INDEX-1}}].javaFormRegularMessage" value="{{d.javaFormRegularMessage}}"></div>'}
            ]]
        });

        //监听单元格编辑
        table.on('edit(table-view)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            // layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value, {
            //     offset: '15px'
            // });
        });
        var form = layui.form;
        form.on('switch(switchTest)', function(data){
            //layer.tips('开关checked：'+ (this.checked ? 'true' : 'false'+' '+this.value), data.othis);
            this.value = (this.checked ? true : false);
            data.value=(this.checked ? true : false);
        });
        // 搜索按钮点事件
        $('#search-btn').on('click', function(){
            layer.load(1);
            table.reload('table-code', {
                where: {tableName: $('#tableNameInput').val()}
                ,done: function(res, curr, count){
                    if(count>0){
                        var tableNameStr = $('#tableNameInput').val();
                        $('#buildCodeTableName').val(tableNameStr);
                        tableNameStr = tableNameStr.replace("rf_","");
                        $('#buildCodeObjectName').val(transformHump(tableNameStr));
                        $('#buildCodeSequenceName').val("seq_"+tableNameStr);
                        $("#buildCodePackageName").val("cn.com.gc.hsz.admin.user");
                    }
                    table.reload('table-view', {
                        data: res.data,
                        limit: count
                    });
                    table.reload('table-edit', {
                        data: res.data,
                        limit: count
                    });

                    layer.closeAll('loading');
                }
            });
        });

        // 校验初始化
        var form = layui.form;
        form.verify({
            buildCodeSequenceName: function (value, item) {
                if ($('#buildCodeAutoSeq').val() && !value) {
                    return '主键类型是自增长时，序列名字不能为空';
                }
            }
            , buildCodePackageName: function (value, item) {
                if (!value) {
                    return '类的包名地址不能为空';
                } else if (value.indexOf("/") > -1 || value.indexOf(".") == -1) {
                    return '类的包名地址不正确，请输入包的逻辑地址以“.”区分包的方式';
                }
            }
            , buildCodeDescription: function (value, item) {
                if (!value) {
                    return '描述信息不能为空';
                }
            }
        });
        form.on('submit(*)', function(){
            $.post('${ctx}/buildCode/saveBuildCode', $('form').serialize(), function(result){
                if(result.code == 0){
                    parent.layer.alert('操作成功', {
                        yes: function(index){
                            parent.layer.close(index);
                            window.location.reload();
                        }
                    });
                }else{
                    parent.layer.msg(result.msg || '操作失败');
                }
            });
            return false;
        });
        form.on('select(module)', function(data){
            $("#buildCodePackageName").attr("value","cn.com.gc.hsz.admin."+data.value);
        });
    });
</script>
</body>
</html>